package com.csun.nusing.ui.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.AllDeviceAlertDeviceJsonBean;
import com.csun.nusing.bean.jsonbean.AllDeviceAlertDeviceJsonBean.AlertDeviceDataBean.AlertDeviceListBean;
import com.csun.nusing.bean.jsonbean.AllDeviceFollowDeviceJsonBean;
import com.csun.nusing.bean.jsonbean.AllDeviceFollowDeviceJsonBean.FollowDeviceDataBean.FollowDeviceListBean;
import com.csun.nusing.bean.jsonbean.AllDeviceSleepDeviceJsonBean;
import com.csun.nusing.bean.jsonbean.AllDeviceSleepDeviceJsonBean.SleepDeviceDataBean.SleepDeviceListBean;
import com.csun.nusing.bean.jsonbean.AllDeviceWatchDeviceJsonBean;
import com.csun.nusing.bean.jsonbean.AllDeviceWatchDeviceJsonBean.WatchDeviceDataBean.WatchDeviceListBean;
import com.csun.nusing.presenter.CommonPresenter;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.adapter.MyBaseAdapter;
import com.csun.nusing.ui.adapter.ViewHolder;
import com.csun.nusing.util.SPUtils;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 所以设备界面 ClassName: AllDeviceActivity
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-11-2
 */
public class AllDeviceActivity extends BaseActivity<CommonPresenter> {

	private ListView lv;
	private String type;
	private String authorization;
	protected AllDeviceFollowDeviceJsonBean alldevicejsonbean;
	protected ImageView iconiv;
	protected TextView codetv;
	protected TextView nametv;
	protected TextView timetv;
	protected AllDeviceWatchDeviceJsonBean alldevicewatchjsonbean;
	protected AllDeviceAlertDeviceJsonBean alldevicealarmjsonbean;
	protected AllDeviceSleepDeviceJsonBean alldevicesleepjsonbean;
	protected SimpleDateFormat sdf;
	protected Date date;
	private TextView titletv;

	@Override
	public int getContentViewId() {
		// TODO Auto-generated method stub
		return R.layout.activity_all_device_list;
	}

	@Override
	public void initSubViews(View view) {
		// TODO Auto-generated method stub
		super.initSubViews(view);
		initView();
	}

	private void initView() {
		titletv = findViewById(R.id.nursing_all_device_title_tv);
		lv = findViewById(R.id.activity_alldevice_list);
	}

	@Override
	public void initData() {
		super.initData();
		sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
		authorization = (String) SPUtils.get(this, "authorization", "");
		type = getIntent().getStringExtra("type");
		if (type.equals("0")) {
			// 随访仪
			requestFollowData();
			titletv.setText("选择随访仪设备");
		} else if (type.equals("3")) {
			// 报警器
			requestAlarmData();
			titletv.setText("选择报警器设备");
		} else if (type.equals("1")) {
			// watch
			requestWatchData();
			titletv.setText("选择智能手表设备");
		} else if (type.equals("2")) {
			// 床垫
			requestSleepData();
			titletv.setText("选择智能床垫设备");
		}
	}

	/**
	 * 加载随访仪数据
	 * 
	 * @Description: TODO
	 * @param @param list
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-3
	 */
	private void requestFollowData() {
		Map<String, String> param = new HashMap<String, String>();
		param.put("pageNo", "1");
		param.put("pageSize", "10000");

		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "-------" + result);
							Gson gson = new Gson();
							alldevicejsonbean = gson.fromJson(result,
									AllDeviceFollowDeviceJsonBean.class);
							if (alldevicejsonbean != null) {
								if (alldevicejsonbean.getData() != null) {
									alldevicejsonbean.getData().getList();
									controlfollowList(alldevicejsonbean
											.getData().getList());
								}
							}
						}
					}
				}).start(UrlBean.DEVICEFOLLOWUPSHOWURL);
	}

	/**
	 * 随访仪的数据
	 * 
	 * @Description: TODO
	 * @param @param list
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-2
	 */
	protected void controlfollowList(final List<FollowDeviceListBean> list) {
		MyBaseAdapter<FollowDeviceListBean> myBaseAdapter = new MyBaseAdapter<FollowDeviceListBean>(
				this, list, R.layout.item_device_list) {
			@Override
			public void convert(ViewHolder helper,int position, FollowDeviceListBean item) {
				iconiv = helper
						.getView(R.id.item_device_list_device_icon);
				codetv = helper
						.getView(R.id.item_device_list_device_code_tv);
				nametv = helper
						.getView(R.id.item_device_list_device_name_tv);
				timetv = helper
						.getView(R.id.item_device_list_device_time_tv);
				iconiv.setImageResource(R.drawable.shebei_icon_sfy_hg);
				codetv.setText("随访仪      " + item.getDeviceNo()!=null?item.getDeviceNo():"-");
				nametv.setText("" + item.getDeviceInfo()!=null?item.getDeviceInfo():"-");
				date = new Date(item.getCreateTime());
				timetv.setText(sdf.format(date));
			}
		};
		lv.setAdapter(myBaseAdapter);

		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(AllDeviceActivity.this,
						FollowOldSelectActivity.class);
				intent.putExtra("deviceId", ""+list.get(position).getId());
				intent.putExtra("deviceMacAddress", ""+list.get(position).getDeviceMac().split(";")[0]);
				startActivity(intent);
			}
		});

	}

	/**
	 * 加载手表数据
	 * 
	 * @Description: TODO
	 * @param @param list
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-3
	 */
	private void requestWatchData() {
		Map<String, String> param = new HashMap<String, String>();
		param.put("pageNo", "1");
		param.put("pageSize", "10000");

		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "-------" + result);
							Gson gson = new Gson();
							alldevicewatchjsonbean = gson.fromJson(result,
									AllDeviceWatchDeviceJsonBean.class);
							if (alldevicewatchjsonbean != null) {
								if (alldevicewatchjsonbean.getData() != null) {
									alldevicewatchjsonbean.getData().getList();
									controlwatchList(alldevicewatchjsonbean
											.getData().getList());
								}
							}
						}
					}
				}).start(UrlBean.DEVICEWATCHSHOWURL);
	}

	/**
	 * 加载手表数据 显示
	 * 
	 * @Description: TODO
	 * @param @param list
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-3
	 */
	protected void controlwatchList(final List<WatchDeviceListBean> list) {
		MyBaseAdapter<WatchDeviceListBean> myBaseAdapter = new MyBaseAdapter<WatchDeviceListBean>(
				this, list, R.layout.item_device_list) {
			@Override
			public void convert(ViewHolder helper,int position, WatchDeviceListBean item) {
				iconiv = helper
						.getView(R.id.item_device_list_device_icon);
				codetv = helper
						.getView(R.id.item_device_list_device_code_tv);
				nametv = helper
						.getView(R.id.item_device_list_device_name_tv);
				timetv = helper
						.getView(R.id.item_device_list_device_time_tv);
				iconiv.setImageResource(R.drawable.shebei_icon_shoubiao_hg);
				codetv.setText("手表      " + item.getDeviceNo()!=null?item.getDeviceNo():"-");
				nametv.setText("" + item.getName()!=null?item.getName():"-");
				date = new Date(item.getCreateTime());
				timetv.setText(sdf.format(date));
			}
		};
		lv.setAdapter(myBaseAdapter);

		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(AllDeviceActivity.this,
						WatchDetailActivity.class);
				intent.putExtra("deviceId", ""+list.get(position).getId());
				startActivity(intent);
			}
		});
	}

	/**
	 * 加载报警器数据 显示
	 * 
	 * @Description: TODO
	 * @param @param list
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-3
	 */
	private void requestAlarmData() {
		Map<String, String> param = new HashMap<String, String>();
		param.put("pageNo", "1");
		param.put("pageSize", "10000");

		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "-------" + result);
							Gson gson = new Gson();
							alldevicealarmjsonbean = gson.fromJson(result,
									AllDeviceAlertDeviceJsonBean.class);
							if (alldevicealarmjsonbean != null) {
								if (alldevicealarmjsonbean.getData() != null) {
									alldevicealarmjsonbean.getData().getList();
									controlalarmList(alldevicealarmjsonbean
											.getData().getList());
								}
							}
						}
					}
				}).start(UrlBean.DEVICEALERTSHOWURL);
	}

	/**
	 * 加载报警器数据 显示
	 * 
	 * @Description: TODO
	 * @param @param list
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-3
	 */
	protected void controlalarmList(final List<AlertDeviceListBean> list) {
		MyBaseAdapter<AlertDeviceListBean> myBaseAdapter = new MyBaseAdapter<AlertDeviceListBean>(
				this, list, R.layout.item_device_list) {
			@Override
			public void convert(ViewHolder helper,int position, AlertDeviceListBean item) {
				iconiv = helper
						.getView(R.id.item_device_list_device_icon);
				codetv = helper
						.getView(R.id.item_device_list_device_code_tv);
				nametv = helper
						.getView(R.id.item_device_list_device_name_tv);
				timetv = helper
						.getView(R.id.item_device_list_device_time_tv);
				iconiv.setImageResource(R.drawable.shebei_icon_baojing_hg);
				codetv.setText("报警器      " + item.getDeviceNo());
				nametv.setText("" + item.getDeviceNo());
				date = new Date(item.getCreateTime());
				timetv.setText(sdf.format(date));
			}
		};
		lv.setAdapter(myBaseAdapter);

		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(AllDeviceActivity.this,
						AlarmDetailActivity.class);
				intent.putExtra("deviceId", ""+list.get(position).getId());
				startActivity(intent);
			}
		});
	}

	private void requestSleepData() {
		Map<String, String> param = new HashMap<String, String>();
		param.put("pageNo", "1");
		param.put("pageSize", "10000");

		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "-------" + result);
							Gson gson = new Gson();
							alldevicesleepjsonbean = gson.fromJson(result,
									AllDeviceSleepDeviceJsonBean.class);
							if (alldevicesleepjsonbean != null) {
								if (alldevicesleepjsonbean.getData() != null) {
									alldevicesleepjsonbean.getData().getList();
									controlsleepList(alldevicesleepjsonbean
											.getData().getList());
								}
							}
						}
					}
				}).start(UrlBean.DEVICESLEEPSHOWURL);
	}

	protected void controlsleepList(final List<SleepDeviceListBean> list) {
		MyBaseAdapter<SleepDeviceListBean> myBaseAdapter = new MyBaseAdapter<SleepDeviceListBean>(
				this, list, R.layout.item_device_list) {
			@Override
			public void convert(ViewHolder helper,int position, SleepDeviceListBean item) {
				iconiv = helper
						.getView(R.id.item_device_list_device_icon);
				codetv = helper
						.getView(R.id.item_device_list_device_code_tv);
				nametv = helper
						.getView(R.id.item_device_list_device_name_tv);
				timetv = helper
						.getView(R.id.item_device_list_device_time_tv);
				iconiv.setImageResource(R.drawable.shebei_icon_chuangdian_hg);
				codetv.setText("床垫      " + item.getDeviceNo()!=null?item.getDeviceNo():"-");
				nametv.setText("" + item.getDeviceInfo()!=null?item.getDeviceInfo():"-");
				date = new Date(item.getCreateTime());
				timetv.setText(sdf.format(date));
			}
		};
		lv.setAdapter(myBaseAdapter);

		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(AllDeviceActivity.this,
						BedDetailActivity.class);
				intent.putExtra("deviceId", ""+list.get(position).getId());
				startActivity(intent);
			}
		});
	}

}
